package com.worthtech.billportal.dao.hibernate;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.worthtech.billportal.bean.CsDicBankcomm;
import com.worthtech.billportal.bean.CsDicBelongcomm;
import com.worthtech.billportal.bean.CsDicCardtype;
import com.worthtech.billportal.bean.CsDicEnterprisetype;
import com.worthtech.billportal.bean.CsDicFactory;
import com.worthtech.billportal.bean.CsDicGangwaycomm;
import com.worthtech.billportal.bean.CsDicInstallcomm;
import com.worthtech.billportal.bean.CsDicLocation;
import com.worthtech.billportal.bean.CsDicMenu;
import com.worthtech.billportal.bean.CsDicProject;
import com.worthtech.billportal.bean.CsDicRentcomm;
import com.worthtech.billportal.bean.CsDicTerminaltype;
import com.worthtech.billportal.dao.ParamDAO;
import com.worthtech.billportal.exception.SCException;

public class ParamHibernateDAO extends HibernateDaoSupport implements ParamDAO {

	public String[] getQueryParam(String type) {
		String[] queryParam = new String[2];
		if (type.equals("1")) {
			queryParam[0] = "CsDicGangwaycomm";
			queryParam[1] = "commercecode";
		} else if (type.equals("2")) {
			queryParam[0] = "CsDicBelongcomm";
			queryParam[1] = "commercecode";
		} else if (type.equals("3")) {
			queryParam[0] = "CsDicRentcomm";
			queryParam[1] = "commercecode";
		} else if (type.equals("4")) {
			queryParam[0] = "CsDicInstallcomm";
			queryParam[1] = "commercecode";
		} else if (type.equals("5")) {
			queryParam[0] = "CsDicMenu";
			queryParam[1] = "menuCode";
		} else if (type.equals("6")) {
			queryParam[0] = "CsDicProject";
			queryParam[1] = "procode";
		} else if (type.equals("7")) {
			queryParam[0] = "CsDicTerminaltype";
			queryParam[1] = "procode";
		} else if (type.equals("8")) {
			queryParam[0] = "CsDicFactory";
			queryParam[1] = "procode";
		} else if (type.equals("9")) {
			queryParam[0] = "CsDicCardtype";
			queryParam[1] = "procode";
		} else if (type.equals("10")) {
			queryParam[0] = "CsDicEnterprisetype";
			queryParam[1] = "procode";
		} else if (type.equals("11")) {
			queryParam[0] = "CsDicLocation";
			queryParam[1] = "procode";
		} else if (type.equals("12")) {
			queryParam[0] = "CsDicBankcomm";
			queryParam[1] = "commercecode";
		} else {
			queryParam[0] = "";
			queryParam[1] = "";
		}
		return queryParam;
	}
	
	public List getListByHQL(String tableName, int startPage, int perNo,
			String orderby) {
		String hql = "from " + tableName + " " + orderby;
		System.out.println("hql2=" + hql);
		if(perNo==0){
			List l = this.getHibernateTemplate().find(hql);
			System.out.println("size = "+l.size());
			return l;
		}
		return this.getSession().createQuery(hql).setFirstResult(startPage)
				.setMaxResults(perNo).list();
	}

	public Object getParam(String id, String type) {
		String[] queryParam = getQueryParam(type);
		String sql;
		if(type.equals("5")){
		sql = "from " + queryParam[0] + " where " + queryParam[1]
		                              								+ " like '"+id+"%'";
		}else{
			sql = "from " + queryParam[0] + " where " + queryParam[1]
		                              								+ " = '"+id+"'";
		}
		System.out.println(sql);
		List list = this.getHibernateTemplate()
				.find(sql);
		if (list == null || list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}

	public List getList(String type) {
		String[] queryParam = getQueryParam(type);
		List list = this.getHibernateTemplate().find(
				"from " + queryParam[0] + "", null);
		return list;
	}
	public void update(Object o){
		this.getHibernateTemplate().update(o);
	}

	public void updateInfo(Object paramObj,String type) throws SCException {
		if(type.equals("1"))
		{
			this.getHibernateTemplate().update(paramObj);
		}
		else if(type.equals("2"))
		{
			CsDicBelongcomm param = new CsDicBelongcomm();
			this.getHibernateTemplate().update(paramObj);
		}
		else if(type.equals("3"))
		{
			CsDicRentcomm param = new CsDicRentcomm();
			this.getHibernateTemplate().update(paramObj);
		}
		else if(type.equals("4"))
		{
			CsDicInstallcomm param = new CsDicInstallcomm();
			this.getHibernateTemplate().update(paramObj);
		}
		else if(type.equals("5"))
		{
			CsDicMenu param = new CsDicMenu();
			this.getHibernateTemplate().update(paramObj);
		}
		else if(type.equals("6"))
		{
			CsDicProject param = new CsDicProject();
			this.getHibernateTemplate().update(paramObj);
		}
		else if(type.equals("7"))
		{
			CsDicTerminaltype param = new CsDicTerminaltype();
			this.getHibernateTemplate().update(paramObj);
		}
		else if(type.equals("8"))
		{
			CsDicFactory param = new CsDicFactory();
			this.getHibernateTemplate().update(paramObj);
		}
		else if(type.equals("9"))
		{
			CsDicCardtype param = new CsDicCardtype();
			this.getHibernateTemplate().update(paramObj);
		}
		else if(type.equals("10"))
		{
			CsDicEnterprisetype param = new CsDicEnterprisetype();
			this.getHibernateTemplate().update(paramObj);
		}
		else if(type.equals("11"))
		{
			CsDicLocation param = new CsDicLocation();
			this.getHibernateTemplate().update(paramObj);
		}
		else if(type.equals("12"))
		{
			CsDicBankcomm param = new CsDicBankcomm();
			this.getHibernateTemplate().update(paramObj);
		}
	}	



	public String getCount(String tableName, String condition) {
		String hql = "select count(*) from " + tableName + " " + condition;
		System.out.println("hql1=" + hql);
		String tmp = this.getSession().createQuery(hql).list().get(0)
				.toString();
		System.out.println("count=" + tmp);
		return tmp;
	}

	public void saveInfo(Object paramobj,String type) {
		System.out.println("type="+type);
		if(type.equals("1"))
		{
			CsDicGangwaycomm param = new CsDicGangwaycomm();
			this.getHibernateTemplate().save(paramobj);
		}
		else if(type.equals("2"))
		{
			CsDicBelongcomm param = new CsDicBelongcomm();
			this.getHibernateTemplate().save(paramobj);
		}
		else if(type.equals("3"))
		{
			CsDicRentcomm param = new CsDicRentcomm();
			this.getHibernateTemplate().save(paramobj);
		}
		else if(type.equals("4"))
		{
			CsDicInstallcomm param = new CsDicInstallcomm();
			this.getHibernateTemplate().save(paramobj);
		}
		else if(type.equals("5"))
		{
			CsDicMenu param = new CsDicMenu();
			this.getHibernateTemplate().save(paramobj);
		}
		else if(type.equals("6"))
		{
			CsDicProject param = new CsDicProject();
			this.getHibernateTemplate().save(paramobj);
		}
		else if(type.equals("7"))
		{
			CsDicTerminaltype param = new CsDicTerminaltype();
			this.getHibernateTemplate().save(paramobj);
		}
		else if(type.equals("8"))
		{
			CsDicFactory param = new CsDicFactory();
			this.getHibernateTemplate().save(paramobj);
		}
		else if(type.equals("9"))
		{
			CsDicCardtype param = new CsDicCardtype();
			this.getHibernateTemplate().save(paramobj);
		}
		else if(type.equals("10"))
		{
			CsDicEnterprisetype param = new CsDicEnterprisetype();
			this.getHibernateTemplate().save(paramobj);
		}
		else if(type.equals("11"))
		{
			CsDicLocation param = new CsDicLocation();
			this.getHibernateTemplate().save(paramobj);
		}
		else if(type.equals("12"))
		{
			CsDicBankcomm param = new CsDicBankcomm();
			this.getHibernateTemplate().save(paramobj);
		}
	}

	public void delInfo(Object paramobj,String type) {
		this.getHibernateTemplate().delete(paramobj);
	}

	public String getSeqID(String seqname) {
		return this.getSession().createSQLQuery(
				"SELECT " + seqname + ".NEXTVAL FROM USER_SEQUENCES").list()
				.get(0).toString();
	}


}
